//
//  HMOrginalView.swift
//  HMWeiBo
//
//  Created by Apple on 16/8/1.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

let HomeCellMargin = 10

class HMOrginalView: UIView {

    var constarit: Constraint?
    
    // 定义属性
    var orginalViewModel: HMHomeCellViewModel?{
    
        didSet{
        
            // 微博个人信息
//            nameLabel.text = orginalViewModel?.statusModel?.user?.screen_name
            nameLabel.text = orginalViewModel?.screen_name
            
            // 设置头像
            photoImageView.sd_setImageWithURL(orginalViewModel?.photoUrl, placeholderImage: UIImage(named: "avatar_default_big"))
            
            //  认证ImageView就需要一个Image,我们把判断的逻辑放在VIewModel
            verfitiedImageView.image = orginalViewModel?.verfited_Image
            // 设置等级图片
            levelImageView.image = orginalViewModel?.level_Image
            // 微博内容
//            contentLabel.text = orginalViewModel?.statusModel?.text
            let text = orginalViewModel?.statusModel?.text
            
            contentLabel.attributedText = orginalViewModel?.dealEmoticon(text)
            
            sourceLabel.text =  orginalViewModel?.source_String
            
           
            orginalViewModel?.dealTime()
//            printLog(orginalViewModel?.statusModel?.created_at)
//
//            
//            orginalViewModel?.statusModel?.source.comp
            
            // 1. 如果微博是 原创微博有配图,则数据在 原创微博的pic_urls字段里;
            // 2. 如果微博是 转发微博,则 原创微博数据字段里 pic_urls没有数据,数据会放在 retweet_statu的 pic_urls
            
            constarit?.uninstall()
            // 判断之前, self.make.bottom.equalTo(pictureView.snp_bottom)
            // 有这个约束,
            // 解决思路: 让之前的这个约束,卸载. 
            // 再根据数据,重新添加约束
            
            // 我们根据数据来判断一下,没有配图的cell,不应该展示配图
            // 1. 先把数据变成非可选的
            // 2. 判断一下数组的个数要大于0
            if let pic_urls = orginalViewModel?.statusModel?.pic_urls where pic_urls.count > 0{
            
                // 说明有配图
                pictureView.hidden = false
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                   self.constarit = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
                
                // 传递数据
                pictureView.pic_urls = pic_urls
                
            }else {
                
                // 没有配图
                pictureView.hidden = true
                
                // 第二步: 配图的位置还在;配图的高度=0; 修改视图的约束,让配图的高度不在自动计算行高之列
                self.snp_updateConstraints(closure: { (make) -> Void in
                   self.constarit = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
                
            }

        }
        
    }
    
    // 第一步: 重写父类的构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    /*
        OC里把约束写在了 init 里,没有起作用.他把约束写在了  layoutsubViews里,导致问题: 需要多次滑动tableView才能显示 正确的约束
        建议: 除非迫不得已,可以在 layoutsubViews 设置约束,修改子控件的frame,其他的情况,不建议在  layoutsubViews 写任何代码
            在赋值的 set方法里修改约束
    
    
    */
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 第二步: 定义一个添加子类和设置子类控件约束的方法
    private func setupUI(){
        
        addSubview(photoImageView)
        addSubview(nameLabel)
        addSubview(verfitiedImageView)
        addSubview(levelImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        // 一定要先把视图添加到 view里,再添加约束
        
        // 添加约束
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(HomeCellMargin)
            make.top.equalTo(self.snp_top).offset(HomeCellMargin)
            
            make.width.height.equalTo(50)
        }
        
        verfitiedImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(photoImageView.snp_right)
            make.centerY.equalTo(photoImageView.snp_bottom)
            
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(HomeCellMargin)
            make.top.equalTo(photoImageView.snp_top)
        }
        
        levelImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(HomeCellMargin)
            make.centerY.equalTo(nameLabel.snp_centerY)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(HomeCellMargin)
            make.top.equalTo(nameLabel.snp_bottom).offset(HomeCellMargin)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(timeLabel.snp_centerY)
            make.left.equalTo(timeLabel.snp_right).offset(HomeCellMargin)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self.snp_left)
            make.top.equalTo(photoImageView.snp_bottom).offset(HomeCellMargin)
            make.right.equalTo(self.snp_right)
//            make.bottom.equalTo(self.snp_bottom)
            
        }
        
      // 配图的约束该怎么添加? 按照转发微博的实现思路来
        /*
            1. 我们认为所有的微博都有配图
            2. 我们再根据数据,没有微博配图的,让微博配图隐藏或者高度=0
        
        */
        
        pictureView.snp_makeConstraints { (make) -> Void in
         
            make.left.equalTo(self.snp_left).offset(HomeCellMargin)
            make.top.equalTo(contentLabel.snp_bottom)
            
            // 宽和高的约束,我们是不是不知道?
            // 我们为了验证思路,可以先随便给一个
            make.width.equalTo(100)
            make.height.equalTo(100)
            
            
        }
        
        // 告诉self它的底部在哪里,这样才可以自动计算行高
        self.snp_makeConstraints { (make) -> Void in
           self.constarit = make.bottom.equalTo(pictureView.snp_bottom).constraint
        }
    }
    
    //第三步: 懒加载控件
    //MARK:-- 懒加载控件
    
    private lazy var photoImageView: UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_default_big")
        
        return img
    }()
    
    /// 认证图片
    private lazy var verfitiedImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_enterprise_vip")
        
        return img
    }()
    
    private lazy var nameLabel: UILabel = {
    
        let label = UILabel()
        
        label.text = "名字"
        label.font = UIFont.systemFontOfSize(15)
        label.textColor = UIColor.darkGrayColor()
        
        
        
        return label
    }()
    
    /// 等级
    private lazy var levelImageView: UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "common_icon_membership_level1")
        
        return img
    }()
    
    ///时间
    private lazy var timeLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "时间"
        label.font = UIFont.systemFontOfSize(13)
        label.textColor = UIColor.orangeColor()
        
        
        return label
    }()
    
    ///来源
//    private lazy var sourceLabel: UILabel = {
//        
//        let label = UILabel()
//        
//        label.text = "来源"
//        label.font = UIFont.systemFontOfSize(13)
//        label.textColor = UIColor.darkGrayColor()
//        
//        return label
//    }()
    
    private lazy var sourceLabel: UILabel = UILabel(text: "来源", fontSize: 13, color: UIColor.darkGrayColor())
    
    ///内容
    private lazy var contentLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "文件而反过来看就按了沙发了开始进而发生看来减肥"
        label.font = UIFont.systemFontOfSize(16)
        label.textColor = UIColor.blackColor()
        
        label.numberOfLines = 0
        
        return label
    }()

    
    private lazy var pictureView: HMPhotoView = HMPhotoView()

}
